//
//  SISSTools.m
//  SISSAPP
//
//  Created by 优房网 on 2017/3/21.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSTools.h"

@interface SISSTools ()

/* imageView */
@property (weak, nonatomic) UIImageView *imageView;
/* coverView */
@property (weak, nonatomic) UIView *coverView;
/* effectview */
@property (weak, nonatomic) UIVisualEffectView *effectview;

@end
@implementation SISSTools


#pragma mark - 用户
+ (BOOL)currentID {
    return [SISSUserDefaultsTool objetForKey:CURRENTID] ? YES : NO;
}

+ (BOOL)currentLoginState {
    return [SISSUserDefaultsTool objetForKey:CURRENTLOGIN] ? YES : NO;
}

#pragma mark - 弹框
//showSvBase
+ (void)showSV:(NSString *)content andTime:(float)delay andColor:(BOOL)isColor andType:(NSString *)type{
    
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    
    UIColor *color = [UIColor colorWithWhite:0 alpha:0.5];
    if (!isColor) {//没有登录时
        color = [UIColor clearColor];
    }
    
    [SVProgressHUD setBackgroundColor:color];
//    NSBundle *bundle = [NSBundle bundleForClass:[SVProgressHUD class]];
//    NSURL *url = [bundle URLForResource:@"SVProgressHUD" withExtension:@"bundle"];
//    NSBundle *imageBundle = [NSBundle bundleWithURL:url];
    //根据不同类型显示不同类型的弹框SV COMMONSV ERRORSV SUCCESSSV
    if ([type isEqualToString:COMMONSV]) {
        
        // 设置图片
        NSMutableArray *imageArray = [NSMutableArray array];
        
        for (int i = 0; i < 40; i ++) {
            
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading%d",i+1]];
            [imageArray addObject:image];
        }
        [SVProgressHUD setImageViewSize:CGSizeMake(88, 44)];
        [SVProgressHUD setInfoImage:[UIImage animatedImageWithImages:imageArray duration:2.0]];
        [SVProgressHUD showInfoWithStatus:[NSString stringWithFormat:@"%@",content]];
        
    } else if ([type isEqualToString:ERRORSV]){
//        [SVProgressHUD setInfoImage:nil];
        [SVProgressHUD showInfoWithStatus:[NSString stringWithFormat:@"%@",content]];
        
    } else if ([type isEqualToString:SUCCESSSV]){
        [SVProgressHUD setInfoImage:[UIImage imageNamed:@"success"]];
        [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"%@",content]];
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
    });
}

+ (void)showSVTitle:(NSString *)content andTime:(float)delay andColor:(BOOL)isColor{
    [SISSTools showSV:content andTime:delay andColor:isColor andType:COMMONSV];
}

+ (void)showSVErrorTitle:(NSString *)content andTime:(float)delay andColor:(BOOL)isColor{
    [SISSTools showSV:content andTime:delay andColor:isColor andType:ERRORSV];
}

+ (void)showSVSuccessTitle:(NSString *)content andTime:(float)delay andColor:(BOOL)isColor{
    [SISSTools showSV:content andTime:delay andColor:isColor andType:SUCCESSSV];
}

+ (void)showSVWithTitle:(NSString *)content {
    
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    
    UIColor *color = [UIColor colorWithWhite:0 alpha:0.5];
//    [SVProgressHUD setInfoImage:[UIImage new]];
    [SVProgressHUD setBackgroundColor:color];
    [SVProgressHUD showInfoWithStatus:content];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
    });
}

+ (void)dissmiss {
    [SVProgressHUD dismiss];
}

#pragma mark - 创建网络请求时的遮盖
- (void)creatCoverView {
    
    //添加弹框
    [SISSTools showSVTitle:@"数据加载中..." andTime:30 andColor:NO];
    
    //创建遮罩图
    UIView *coverView = [[UIView alloc]initWithFrame:SCREEN_BOUNDS];
    //将覆盖到window上
    [[UIApplication sharedApplication].keyWindow addSubview:coverView];
    
    //添加图片
    UIImage *img = [UIImage imageNamed:@"background"];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:img];
    //    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.frame = SCREEN_BOUNDS;
    [coverView addSubview:imageView];
    
    //添加磨玻璃效果
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    effectview.frame = SCREEN_BOUNDS;
//    [imageView addSubview:effectview];
    
    self.imageView = imageView;
    self.coverView = coverView;
    self.effectview = effectview;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [SISSTools showSVTitle:@"数据加载失败" andTime:0.1 andColor:NO];
        [self removeCoverView];
    });
}


- (void)removeCoverView {
    
    [SISSTools dissmiss];
    //销毁
    [self.imageView removeFromSuperview];
    [self.coverView removeFromSuperview];
//    [self.effectview removeFromSuperview];
}



@end
